package com.taobao.machinesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.common.Result;
import com.taobao.machinesystem.common.SystemConst;
import com.taobao.machinesystem.entity.McAddressbook;
import com.taobao.machinesystem.entity.SysUser;
import com.taobao.machinesystem.entity.vo.McAddressbookQueryVo;
import com.taobao.machinesystem.service.McAddressbookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 第三方公司通讯录 前端控制器
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@RestController
@RequestMapping("/machine_system/mc-addressbook")
public class McAddressbookController {

    @Autowired
    private McAddressbookService mcAddressbookService;

    /**
     * 创建联系人
     * @param mcAddressbook
     * @return Result
     */
    @PostMapping("/create")
    public Result createMcAddressBook(@RequestBody McAddressbook mcAddressbook) {
        System.out.println(mcAddressbook);
        System.out.println("00000000");
        if (mcAddressbook==null) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        mcAddressbook.setCreatedBy(sysUser.getUsername());
        boolean flag = mcAddressbookService.save(mcAddressbook);
        if (flag) {
            log.info("create plan success {}", mcAddressbook.toString());
            return Result.ok().setMessage(SystemConst.CREATE_SUCCESS);
        } else {
            log.error("create plan fail {}", mcAddressbook.toString());
            return Result.error().setMessage(SystemConst.CREATE_FAIL);
        }
    }

    /**
     * 分页查询
     * @param currentPage
     * @param size
     * @param mcAddressbookQueryVo
     * @return Result
     */
    @GetMapping("/list/{currentPage}/{size}")
    public Result listMcAddressBook(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("size")Integer size,
            McAddressbookQueryVo mcAddressbookQueryVo){
        Page<McAddressbook> mcAddressbookPage =  mcAddressbookService.listMcAddressbook(currentPage,size,mcAddressbookQueryVo);

        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("total",mcAddressbookPage.getTotal())
                .setMapValue("items",mcAddressbookPage.getRecords());
    }

    /**
     * 跟新操作
     * @param mcAddressBook
     * @return Result
     */
    @PutMapping("/update")
    public Result updateMcAddressBook(@RequestBody McAddressbook mcAddressBook) {
        if (!Objects.isNull(mcAddressBook)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }

        boolean flag = mcAddressbookService.updateById(mcAddressBook);
        if (flag) {
            log.info("update plan0 success {}", mcAddressBook.toString());
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        } else {
            log.error("update plan0 fail {}", mcAddressBook.toString());
            return Result.error().setMessage(SystemConst.UPDATE_FAIL);
        }
    }

    /**
     * 删除计划
     * @param id
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteMcAddressBook(@PathVariable String id){
        boolean flag = mcAddressbookService.removeById(id);
        if (flag) {
            log.info("update plan success {}", id);
            return Result.ok().setMessage(SystemConst.DELETE_SUCCESS);
        } else {
            log.error("update plan fail {}", id);
            return Result.error().setMessage(SystemConst.DELETE_FAIL);
        }
    }
}

